﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SenerPro;

namespace MensUnoShop.Models
{
    public class OrderFactory
    {
        private static DBOrder _repository = new DBOrder();

        /// <summary>
        /// 將購物車轉成訂單
        /// </summary>
        /// <param name="sc"></param>
        /// <returns></returns>
        public static Order TransToOrder(ShoppingCart sc, Guid mguid)
        {
            Order od = new Order(Guid.NewGuid());
            od.TITLE = string.Empty;
            od.DSKLIST = sc.DSKLIST;
            od.MGUID = mguid;
            od.MLIST = sc.MLIST;
            od.TOTALPRICE = sc.MLIST.Sum(m => m.PRICE) + od.DELIVERYCOST;
            od.DELIVERYCOST = sc.DELIVERYCOST;
            od.SKGIFTLIST = sc.SKGIFTLIST ?? new List<SalesKit>();
            od.PAYPRICE = od.TOTALPRICE-od.DSKLIST.Sum(d=>d.DEPRICE);
            od.SCGUID = sc.SCGUID;

            return od;
        }

        public static Order GetTempOrder(string cashid)
        {
            return _repository.GetTempOrder(cashid);
        }

        public static List<SimpleOrder> GetOrderList(Guid mguid)
        {
            return _repository.GetOrderList(mguid);
        }

        /*
        public static List<SimpleOrder> GetSimpleOrderList(Guid mguid, int amount)
        {
            List<SimpleOrder> solist = _repository.GetSimpleOrderList(mguid, amount);
            if (solist != null)
                return solist.ToList<ISimpleArticle>();
            else
                return null;
        }

        public static List<ISimpleArticle> GetSimpleOrderList(Guid mguid)
        {
            List<SimpleOrder> solist = _repository.GetSimpleOrderList(mguid);
            if (solist != null)
                return solist.ToList<ISimpleArticle>();
            else
                return null;
        }

        public static List<SimpleOrder> GetOrderList(Guid mguid)
        {
            return _repository.GetOrderList(mguid);
        }

        public static List<SimpleOrder> GetBonusOrderList(Guid mguid)
        {
            return _repository.GetBonusOrderList(mguid);
        }

        public static Order GetOrderByCashid(string cashid)
        {
            return _repository.GetOrder(cashid, "cashid");
            
        }

        public static Order GetOrderByTxid(string txid)
        {
            return _repository.GetOrder(txid, "txid");

        }
        */

        public static Order GetOrderByCashid(string cashid)
        {
            Order o = _repository.GetOrder(cashid, "cashid");
            if (o != null)
                o.GetStatus();
            return o;
        }

        public static Order GetOrderByTxid(string id)
        {
            Order o = _repository.GetOrder(id, "txid");
            if (o != null)
                o.GetStatus();
            return o;
        }

        public static Order GetOrderByOguid(string id)
        {
            Order o = _repository.GetOrder(id, "oguid");
            if (o != null)
                o.GetStatus();
            return o;
        }

        public static List<Order> GetOrderList(string querychoice, string querycontent)
        {
            DBOrder dbm = new DBOrder();
            List<Order> orderlist = new List<Order>();
            orderlist = dbm.GetOrderList(querychoice, querycontent);

            return orderlist;
        }

        public static List<OrderData> GetOrderdataList(string querychoice, string querycontent, string querytype)
        {
            DBOrder dbm = new DBOrder();
            List<OrderData> orderdatalist = new List<OrderData>();
            orderdatalist = dbm.GetOrderdataList(querychoice, querycontent, querytype);

            return orderdatalist;
        }


        public static List<OrderData> GetOrderdataListPayment()
        {
            DBOrder dbm = new DBOrder();
            List<OrderData> orderdatalist = new List<OrderData>();
            orderdatalist = dbm.GetOrderdataListPayment();

            return orderdatalist;
        }

        public static List<OrderData> GetOrderdataListReturning(DateTime batchstartdate, DateTime batchenddate)
        {
            DBOrder dbm = new DBOrder();
            List<OrderData> orderdatalist = new List<OrderData>();
            orderdatalist = dbm.GetOrderdataListReturning(batchstartdate, batchenddate);

            return orderdatalist;
        }

        public static bool CreateOrderBatchData(string batchid, Guid oguid, DateTime batchdate)
        {
            DBOrder dbo = new DBOrder();
            return dbo.CreateOrderBatchData(batchid, oguid, batchdate);
        }


        public static List<OrderBatch> GetShipBatch(DateTime batchstartdate, DateTime batchenddate)
        {
            DBOrder dbo = new DBOrder();
            return dbo.GetShipBatch(batchstartdate, batchenddate);
        }

        public static List<OrderData> GetOrderdataListShipBatch(string batchid, string sort)
        {
            DBOrder dbo = new DBOrder();
            return dbo.GetOrderdataListShipBatch(batchid, sort);
        }

        public static List<OrderData> GetOrderdata(Guid oguid)
        {
            DBOrder dbo = new DBOrder();
            return dbo.GetOrderdata(oguid);
        }

        public static bool UpdateOrderShippingnum(Guid oguid, string shippingnum)
        {
            DBOrder dbo = new DBOrder();
            return dbo.UpdateOrderShippingnum(oguid, shippingnum);
        }

    }
}